package com.psds.quartz;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;

/**
 * author: cjianquan
 * date: 2016/8/30
 */
@Controller
@RequestMapping("/quartzController")
public class QuartzController {

    @Autowired
    private QuartzService quartzService ;

    @Autowired
    private CommonQuartz commonQuartz;

    @RequestMapping(value = "/goQuartzNav")
    public String goQuartzNav(ModelMap model) {
        return "quartz_page";
    }

    @RequestMapping(value = "/goQuartzList")
    public String goQuartzList(ModelMap model) {
        return "quartz_list";
    }

    @RequestMapping(value = "/goQuartzDetail")
    public String goQuartzDetail(HttpServletRequest request,ModelMap model){
        String id = request.getParameter("id");
        QuartzEntity quartz;
        if(id==null || id.equals("")){
            quartz = new QuartzEntity();
        }else{
            quartz = quartzService.getQuartzById(id);
        }
        model.put("quartz",quartz);
        return "quartz_detail";
    }

    @RequestMapping("/queryQuartzList")
    @ResponseBody
    public String queryQuartzList(HttpServletRequest request, QuartzEntity quartzEntity,BaseDto baseDto){
        if (quartzEntity == null){
            quartzEntity = new QuartzEntity();
        }
        // 如果没有页码,则设置pageNo=1
        if (baseDto.getPageNo() == null || baseDto.getPageNo() < 1) {
            baseDto.setPageNo(1);
        }
        // 设置分页大小
        if (baseDto.getPageSize() == null) {
            baseDto.setPageSize(10);
        }
        Map<String, Object> map = new HashMap<String, Object>();
        List<QuartzEntity> mapList = new ArrayList<QuartzEntity>();
        int iTotal = 0;
        try{
            iTotal = quartzService.queryQuartzCount(quartzEntity);
            if(iTotal > 0){
                int iStart = (baseDto.getPageNo()-1)*baseDto.getPageSize();
                mapList = quartzService.queryQuartzList(quartzEntity, iStart, baseDto.getPageSize());
            }
        } catch(Exception ex) {
            ex.printStackTrace();
        }
        map.put("totalCount", iTotal);
        map.put("items", mapList);
        return JacksonUtil.bean2Json(map);
    }

    @RequestMapping("/saveQuartz")
    public void saveQuartz(HttpServletRequest request,HttpServletResponse response, QuartzEntity quartzEntity) throws Exception{
        response.setContentType("text/html;charset=utf-8");
        String json;
        try {
            String checkMsg = quartzService.checkQuartz(quartzEntity);
            if(!checkMsg.equals("")){
                json = "{\"error\":\""+checkMsg+"\"}";
            }else{
                if (quartzEntity.getId() == null || quartzEntity.getId().equals("")) {

                    quartzEntity.setId(UUID.randomUUID().toString().replaceAll("-",""));
                    quartzService.addQuartz(quartzEntity);
                    json = "{\"success\":\"新增成功\"}";
                } else {
                    QuartzEntity quartzTemp = quartzService.getQuartzById(quartzEntity.getId());
                    ;

                    MyBeanUtils.copyBeanNotNull2Bean(quartzEntity, quartzTemp);
                    quartzService.updateQuartz(quartzTemp);
                    json = "{\"success\":\"更新成功\"}";
                }
            }

        } catch (Exception e) {
            json = "{\"error\":\"保存异常\"}";
        }
        response.getWriter().write(json);
    }

    @RequestMapping("/delQuartz")
    public void delQuartz(HttpServletResponse response,QuartzEntity quartz) throws Exception{
        response.setContentType("text/html;charset=utf-8");
        String id = quartz.getId();
        if(id!=null && !id.equals("")){
            this.quartzService.delQuartzById(id);
            String json = "{\"success\":\"删除成功\"}";
            response.getWriter().write(json);
        }
    }

    @RequestMapping("/setEnable")
    public void setEnable(HttpServletResponse response,QuartzEntity quartz){
        this.quartzService.updateQuartz(quartz);
    }


    @RequestMapping("/execJob")
    public void execJob(HttpServletResponse response,QuartzEntity quartz) throws Exception{
        response.setContentType("text/html;charset=utf-8");
        quartz = this.quartzService.getQuartzById(quartz.getId());
//        CommonQuartz cq = new CommonQuartz();
        Object args[] = new Object[]{quartz.getQuartzJoburl(),quartz.getQuartzJobname()};
        boolean result = commonQuartz.manualRun(args);
        response.getWriter().write(""+result);
    }

}
